/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.service.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.xy.biz.fd.domain.DocumentPermissions;
import com.xy.biz.fd.domain.bo.DocumentBO;
import com.xy.biz.fd.domain.bo.DocumentLogBO;
import com.xy.biz.fd.domain.bo.RepositoryBO;
import com.xy.biz.fd.domain.req.documentFavorite.GetDocumentFavoriteRequest;
import com.xy.biz.fd.manager.DocumentLogManager;
import com.xy.biz.fd.manager.DocumentManager;
import com.xy.lang.domain.Result;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.biz.fd.domain.bo.DocumentFavoriteBO;
import com.xy.biz.fd.domain.query.DocumentFavoriteQuery;
import com.xy.biz.fd.service.DocumentFavoriteService;
import com.xy.biz.fd.manager.DocumentFavoriteManager;
import org.springframework.util.CollectionUtils;

/**
 *
 *
 * @author wwj
 * @date 2021-2-2
 */
@Service
public class DocumentFavoriteServiceImpl implements DocumentFavoriteService, InitializingBean {

    @Autowired
    private DocumentFavoriteManager documentFavoriteManager;
    @Autowired
    private DocumentManager documentManager;
    @Autowired
    private DocumentEventBus documentEventBus;
    @Autowired
    private DocumentLogManager documentLogManager;

    @Override
    public Result<?> saveDocumentFavorite(DocumentFavoriteBO documentFavoriteBO) {
        Long documentId = documentFavoriteBO.getDocumentId();
        DocumentFavoriteQuery query = new DocumentFavoriteQuery();
        query.setDocumentId(documentId);
        query.setCreateUserId(UserContextHolder.getUserId());
        DocumentFavoriteBO documentFavorite = documentFavoriteManager.getDocumentId(query);
//		Boolean deleteFlag = documentFavorite.getDeleteFlag();

//		documentFavoriteManager.listDocumentFavoriteById(documentFavoriteId)
        if (documentFavorite == null) {
            LocalDateTime now = LocalDateTime.now();
            documentFavoriteBO.setCreateTime(now);
            documentFavoriteBO.setDeleteFlag(false);
            int affectedRows = documentFavoriteManager.saveDocumentFavorite(documentFavoriteBO);
            if (affectedRows > 0) {
                DocumentBO document = documentManager.getDocument(documentFavoriteBO.getDocumentId());
                DocumentLogBO documentLogBO = buildDocumentLog(document);
                documentLogBO.setAccessType(DocumentPermissions.FAVORITE);
                documentLogManager.saveDocumentLog(documentLogBO);
                return Result.success();
            }
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> saveAllDocumentFavorite(Collection<DocumentFavoriteBO> collection) {
        int affectedRows = documentFavoriteManager.saveAllDocumentFavorite(collection);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> updateDocumentFavorite(DocumentFavoriteBO documentFavoriteBO) {
        int affectedRows = documentFavoriteManager.updateDocumentFavorite(documentFavoriteBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    public Result<?> updateDocumentFavoritesByIds(Collection<Long> ids) {
        int affectedRows = documentFavoriteManager.updateDocumentFavoritesByIds(ids);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> deleteDocumentFavorite(Long documentFavoriteId) {
        int affectedRows = documentFavoriteManager.deleteDocumentFavorite(documentFavoriteId);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public DocumentFavoriteBO getDocumentFavorite(Long documentFavoriteId) {
        return documentFavoriteManager.getDocumentFavorite(documentFavoriteId);
    }

    @Override
    public List<DocumentFavoriteBO> listDocumentFavoriteById(Collection<Long> collection) {
        return documentFavoriteManager.listDocumentFavoriteById(collection);
    }

    @Override
    public List<DocumentFavoriteBO> listDocumentFavorite(DocumentFavoriteQuery query) {
        return documentFavoriteManager.listDocumentFavorite(query);
    }

    @Override
    public int countDocumentFavorite(DocumentFavoriteQuery query) {
        return documentFavoriteManager.countDocumentFavorite(query);
    }

    @Override
    public Result<?> deleteListDocumentFavorite(Collection<Long> documentFavoriteIds) {
        int affectedRows = documentFavoriteManager.deleteListDocumentFavorite(documentFavoriteIds);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    private DocumentLogBO buildDocumentLog(DocumentBO documentBO) {
        DocumentLogBO documentLogBO = new DocumentLogBO();
        documentLogBO.setDocumentId(documentBO.getDocumentId());
        documentLogBO.setDocumentName(documentBO.getDocumentName());
        documentLogBO.setCreateTime(LocalDateTime.now());
        documentLogBO.setCreateUserId(UserContextHolder.getUserId());
        documentLogBO.setVersion(documentBO.getVersion());
        documentLogBO.setTenantId(UserContextHolder.getTenantId());
        documentLogBO.setDeleteFlag(false);
        return documentLogBO;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        documentEventBus.addDocumentStatusHandler((documentId) -> {
            List<DocumentFavoriteBO> documentFavoriteBOS = documentFavoriteManager.listDocumentFavorite(DocumentFavoriteQuery.max().documentId(documentId).build());
            if (CollectionUtils.isEmpty(documentFavoriteBOS)) {
                return;
            }
            Set<Long> ids = documentFavoriteBOS.stream().map(DocumentFavoriteBO::getDocumentFavoriteId).collect(Collectors.toSet());
            updateDocumentFavoritesByIds(ids);
        });
    }
}
